package com.ruoyi.part.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.part.domain.DmsPartStorageInfo;
import com.ruoyi.part.domain.DmsPartsPurchaseOrder;
import com.ruoyi.part.service.IDmsPartsPurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 零件采购Controller
 * 
 * @author liuchuanfa
 * @date 2024-03-17
 */
@RestController
@RequestMapping("/part/purchase")
public class DmsPartsPurchaseOrderController extends BaseController
{
    @Autowired
    private IDmsPartsPurchaseOrderService dmsPartsPurchaseOrderService;

    /**
     * 查询零件采购列表
     */
    @PreAuthorize("@ss.hasPermi('part:purchase:list')")
    @GetMapping("/list")
    public TableDataInfo list(DmsPartsPurchaseOrder dmsPartsPurchaseOrder)
    {
        startPage();
        List<DmsPartsPurchaseOrder> list = dmsPartsPurchaseOrderService.selectDmsPartsPurchaseOrderList(dmsPartsPurchaseOrder);
        return getDataTable(list);
    }

    /**
     * 导出零件采购列表
     */
    @PreAuthorize("@ss.hasPermi('part:purchase:export')")
    @Log(title = "零件采购", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DmsPartsPurchaseOrder dmsPartsPurchaseOrder)
    {
        List<DmsPartsPurchaseOrder> list = dmsPartsPurchaseOrderService.selectDmsPartsPurchaseOrderList(dmsPartsPurchaseOrder);
        ExcelUtil<DmsPartsPurchaseOrder> util = new ExcelUtil<DmsPartsPurchaseOrder>(DmsPartsPurchaseOrder.class);
        util.exportExcel(response, list, "零件采购数据");
    }

    /**
     * 获取零件采购详细信息
     */
    @PreAuthorize("@ss.hasPermi('part:purchase:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dmsPartsPurchaseOrderService.selectDmsPartsPurchaseOrderById(id));
    }

    /**
     * 根据零件id获取零件信息
     */
    @PreAuthorize("@ss.hasPermi('part:purchase:query')")
    @GetMapping(value = "/partInfo/{orderDetailpartNos}")
    public TableDataInfo getPartsInfoByIds(@PathVariable String[] orderDetailpartNos)
    {
        startPage();
        List<DmsPartStorageInfo> list = dmsPartsPurchaseOrderService.getPartsInfoByIds(orderDetailpartNos);
        return getDataTable(list);
    }

    /**
     * 新增零件采购
     */
    @PreAuthorize("@ss.hasPermi('part:purchase:add')")
    @Log(title = "零件采购", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DmsPartsPurchaseOrder dmsPartsPurchaseOrder)
    {
        return toAjax(dmsPartsPurchaseOrderService.insertDmsPartsPurchaseOrder(dmsPartsPurchaseOrder));
    }

    /**
     * 修改零件采购
     */
    @PreAuthorize("@ss.hasPermi('part:purchase:edit')")
    @Log(title = "零件采购", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DmsPartsPurchaseOrder dmsPartsPurchaseOrder)
    {
        return toAjax(dmsPartsPurchaseOrderService.updateDmsPartsPurchaseOrder(dmsPartsPurchaseOrder));
    }

    /**
     * 删除零件采购
     */
    @PreAuthorize("@ss.hasPermi('part:purchase:remove')")
    @Log(title = "零件采购", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dmsPartsPurchaseOrderService.deleteDmsPartsPurchaseOrderByIds(ids));
    }


    /**
     * 订单配车
     */
    @PreAuthorize("@ss.hasPermi('part:purchase:edit')")
    @Log(title = "订单配车", businessType = BusinessType.UPDATE)
    @PostMapping("/PurchaseVehicleById/{id}")
    public AjaxResult purchaseVehicleById(@PathVariable("id") Long id)
    {
        return toAjax(dmsPartsPurchaseOrderService.purchaseVehicleById(id));
    }
}
